package com.gxc.blogm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.gxc.authcore.system.domain.User;
import com.gxc.authcore.system.util.CurrentUtil;
import com.gxc.blogm.domain.Album;
import com.gxc.blogm.service.AlbumService;
import com.gxc.blogm.service.MediaService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;
import com.gxc.webcore.util.StringUtil;

public class AlbumController {

	MediaService mediaService;
	AlbumService albumService;

	public AlbumService getAlbumService() {
		return albumService;
	}

	public void setAlbumService(AlbumService albumService) {
		this.albumService = albumService;
	}

	public MediaService getMediaService() {
		return mediaService;
	}

	public void setMediaService(MediaService mediaService) {
		this.mediaService = mediaService;
	}

	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("blogm/album/album_list.vm");
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView grid(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = RequestUtil.getInMap(request);
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		PageList pageList = albumService.listPage(map, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	public ModelAndView create(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("blogm/album/album_create.vm");
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = new HashMap();
		User user = CurrentUtil.getAuthUser();
		Album album = (Album) RequestUtil.getInObject(request, Album.class);
		album.setId("album_" + StringUtil.getUUID());
		if (user != null) {
			album.setCreate_user(user.getId());
		}
		album.setCreate_time(new Date());
		album.setUpdate_time(new Date());
		albumService.create(album);
		map.put("status", "success");
		map.put("data", album);
		String result = JsonUtil.object2json(map);
		response.getWriter().write(result);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Album album = albumService.retrieve(id);
		Map model = new HashMap();
		model.put("album", album);
		return new ModelAndView("blogm/album/album_edit.vm", model);
	}

	/**
	 * 修改文章保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView editSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		Album album = (Album) RequestUtil.getInObject(request, Album.class);
		album.setUpdate_time(new Date());
		int i = albumService.update(album);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		// 将album下的文件移至垃圾箱，并删除数据库中的相应记录
		mediaService.deleteByAlbumId(id);

		// 删除album
		int i = albumService.delete(id);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Album album = albumService.retrieve(id);
		Map model = new HashMap();
		model.put("album", album);
		return new ModelAndView("blogm/album/album_view.vm", model);
	}

}
